<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Db;
class Borrow extends Controller
{
	public function index(Request $request)
	{
		$uid = $request->param('uid');
		$callback = $request->param('callback');
		$money = $request->param('howMany');
		$long = $request->param('howLong');
		$huankuan = $request->param('howStill');
		$arr = array(
			"uid"=>$uid,
			"repayMoney"=>$money,
			"long"=>$long,
			"time"=>time(),
			"repayment"=>$huankuan
		);
		$id=Db("borrow")->insertGetId($arr);
		$sql="select * from borrow where bid = $id";
		$data = Db::query($sql);
		foreach($data as $k=>$v){
			if($v['repayment']==1){
				$interestRate = '0.108';
				$emTotal = $v['repayMoney'] * $interestRate / 12 * pow(1 + $interestRate / 12, ($v['long']/30)) / (pow(1 + $interestRate / 12, ($v['long']/30)) - 1); 
				//每月还款金额  
		        $lxTotal = 0; //总利息  
		        for ($i = 0; $i < ($v['long']/30); $i++) {  
		            $lx      = $v['repayMoney'] * $interestRate / 12;   //每月还款利息  
		            $em      = $emTotal - $lx;  //每月还款本金   
		            $v['repayMoney'] = $v['repayMoney'] - $em;  
		            $lxTotal = $lxTotal + $lx;  
		            $data[$k]['evMonth'][]=array("b_id"=>$v['bid'],"periods"=>$i+1,"principal"=>round($em,2),"interest"=>round($lx,2),"sum"=>round($emTotal,2),"sumlx"=>round($lxTotal,2));
				}
			}else if($v['repayment']==2){
				$interestRate = '0.108';
				$emTotal = $v['repayMoney'] * $interestRate / 12 * pow(1 + $interestRate / 12, ($v['long']/30)) / (pow(1 + $interestRate / 12, ($v['long']/30))-1); //每月还款金额
				$lxTotal = 0; //总利息
				for ($i = 0; $i < ($v['long']/30); $i++) {
				$lx      = $v['repayMoney'] * $interestRate / 12;   //每月还款利息
				$em      = $emTotal-$lx;  //每月还款本金
				$v['repayMoney'] = $v['repayMoney']-$em;
				$lxTotal = $lxTotal + $lx;
				$data[$k]['evMonth'][]=array("b_id"=>$v['bid'],"periods"=>$i+1,"principal"=>round($em,2),"interest"=>round($lx,2),"sum"=>round($emTotal,2),"sumlx"=>round($lxTotal,2));
			}
            }		
		}
			$ay=[];
			foreach($data as $k=>$v){
				foreach($v['evMonth'] as $key=>$val){
					$ay[] = array(
						"b_id"=>$val['b_id'],
						"time"=>time(),
						"periods"=>$val['periods'],
						"principal"=>$val['principal'],
						"interest"=>$val['interest'],
						"sum"=>$val['sum']
					);
				}
			}
			$res = Db('payhistory')->insertAll($ay);
		if($res){
          	$Systemmessage = model('systemmessage');
			$title = '借款信息';
	    	$content = '借款成功,请等待审核';
	    	$messageRes = $Systemmessage->insertMessage($uid, $content,$title);
			$arr['status']=200;
			$arr['msg']='借款成功,等待审核';
		}else{
			$arr['status']=300;
			$arr['msg']='借款失败';
		}
		echo callback($callback,$arr);
	}
	public function getData(Request $request)
	{
		$uid = $request->param('uid');
		$callback = $request->param('callback');
		$sql = "select * from borrow where uid = $uid";
		$data = Db::query($sql);
		if(!empty($data)){
			foreach($data as $k=>$v){
				$data[$k]['time']=date("Y-m-d H:i:s",$v['time']);
			}
			foreach($data as $k=>$v){
				if($v['repayStatus']==1){
					$data[$k]['status1'] = '审核中';
				}
				if($v['repayStatus']==2){
					$data[$k]['status1'] = '审核通过';
				}
				if($v['repayStatus']==3){
					$data[$k]['status1'] = '未通过';
				}
			}
			foreach($data as $k=>$v){
				if($v['repayment']==1){
					$data[$k]['fangshi']='等额本金';
				}else if($v['repayment']==2){
					$data[$k]['fangshi']='等额本息';
				}else if($v['repayment']==3){
					$data[$k]['fangshi']='一次性还清';
				}
			}
			$arr['status'] = 200;
			$arr['msg'] = "成功";
			$arr['data']= $data;
		}else{
			$arr['status'] = 300;
			$arr['msg'] = "失败";
		}
		echo callback($callback,$arr);
	}
	public function getHistory(Request $request)
	{
		$bid = $request->param("bid");
		$callback = $request->param('callback');
		$sql="select * from payhistory where b_id = $bid";
		$data = Db::query($sql);
		foreach($data as $key=>$v){
			if($v['status']==1){
				$data[$key]['status1'] = '未还';
			}else if($v['status']==2){
				$data[$key]['status1'] = '已还';
			}
		}
		if(!empty($data)){
			$arr['status'] = 200;
			$arr['msg'] = "成功";
			$arr['data'] = $data;
		}else{
			$arr['status'] = 301;
			$arr['msg'] = "失败";
		}
		echo callback($callback,$arr);
	}
}
